﻿@page "/showprofile"

@using Microsoft.Identity.Web
@using Microsoft.Graph
@inject Microsoft.Graph.GraphServiceClient GraphServiceClient
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler

<h1>Me</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (user == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table table-striped table-condensed" style="font-family: monospace">
        <tr>
            <th>Property</th>
            <th>Value</th>
        </tr>
        <tr>
            <td>Name</td>
            <td>@user.DisplayName</td>
        </tr>
        <tr>
            <td>Photo</td>
            <td>
                @{
                    if (photo != null)
                    {
                        <img style="margin: 5px 0; width: 150px" src="data:image/jpeg;base64, @photo" />
                    }
                    else
                    {
                        <h3>NO PHOTO</h3>
                        <p>Check user profile in Azure Active Directory to add a photo.</p>
                    }
                }
            </td>
        </tr>
    </table>
}

@code {
    User user;
    string photo;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            user = await GraphServiceClient.Me.Request().GetAsync();
            photo = await GetPhoto();
        }
        catch (Exception ex)
        {
            ConsentHandler.HandleException(ex);
        }
    }

    protected async Task<string> GetPhoto()
    {
        string photo;

        try
        {
            using (var photoStream = await GraphServiceClient.Me.Photo.Content.Request().GetAsync())
            {
                byte[] photoByte = ((System.IO.MemoryStream)photoStream).ToArray();
                photo = Convert.ToBase64String(photoByte);
                this.StateHasChanged();
            }

        }
        catch (Exception)
        {
            photo = null;
        }
        return photo;
    }

}
